mini.parse();
var projectName = $("#projectName").val();
var form = new mini.Form("form1");
var grid = mini.get("datagrid1");
var shopId = mini.get("shopId");
var storageId = mini.get("storageId");
var storagePhyId = mini.get("storagePhyId");
var inStorageId = mini.get("inStorageId")
var inStoragePhyId = mini.get("inStoragePhyId")
var shopName = mini.get("shopName");
var storageName = mini.get("storageName");
var storagePhyName = mini.get("storagePhyName");
var inStorageName = mini.get("inStorageName");
var inStoragePhyName = mini.get("inStoragePhyName");
var woNo = mini.get("woNo");
var operType;
if(woNo.getValue()){
	grid.load({woNo:woNo.getValue()});
}

function saveForm(fun) {
	form.validate();
	if (form.isValid() == false)
		return;
	var data = GetDataLoseform();
	if(!data.barcodeList||data.barcodeList.length==0){
		mini.alert("请填写设备清单");
		return;
	}
	 if(!checkPurBarcode(data.barcodeList)){
		 return;
	 }
	 var json = mini.encode(data);
	mini.mask({
        el: document.body,
        cls: 'mini-mask-loading',
        html: '正在保存信息...'
    }); 
	$.ajax({
		url : projectName+"/LoseformAdmin/saveLoseformAndCode",
		type : 'post',
		contentType : "application/json; charset=utf-8",
		dataType : 'json',
		data : json,
		cache : false,
		success : function(data) {
			mini.unmask();
			if (data.status == '1') {
				if(typeof(fun) == "function" ){
					fun();
				}else{
					mini.alert(data.message, "" , function() {
						CloseWindowLoseform("save");
					});
				}
				
			} else if(data.status == '0') {
				mini.alert(data.message);
			} else {
				mini.alert("发生错误!");
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
		    mini.unmask();
			mini.alert(jqXHR.responseText);
			CloseWindowLoseform();
		}
	});
}

//  验证条码
function checkPurBarcode(list){
	var flag = true;
	var businessKey = mini.get("woNo").getValue();
	var jsonList = [];
	for(var i=0; i<list.length; i++){
		var json = {};
		json.businessKey = businessKey;
		json.equipmentType = list[i].eqType;
		json.isDel = "0";
		json.num = list[i].eqNum;
		jsonList.push(json);
	}
	var url = projectName+"/PurBarcodeAdmin/validateCount";
	$.ajax({
		url:url,
		type:"post",
		dataType:"json",
		data:"list="+mini.encode(jsonList),
		cache:false,
		async:false,
		success:function(rs){
			if(rs.status == "1"){
				
			}else {
				mini.alert(rs.message);
				flag = false;
			}
		},error:function(){
			mini.alert("验证条码请求失败");
			flag = false;
		}
	})
	return flag;
}
function submitForm() {
	saveForm(submitFormFunc);
}
	function submitFormFunc() {
	form.validate();
	if (form.isValid() == false)
		return;
	var json = mini.encode(GetDataLoseform());
	mini.mask({
		el: document.body,
		cls: 'mini-mask-loading',
		html: '正在提交信息...'
	}); 
	$.ajax({
		url : projectName+"/LoseformAdmin/submitLoseForm",
		type : 'post',
		contentType : "application/json; charset=utf-8",
		dataType : 'json',
		data : json,
		cache : false,
		success : function(data) {
			mini.unmask();
			if (data.status == '1') {
				mini.alert(data.message, "" , function() {
					CloseWindowLoseform("save");
				});
			} else if(data.status == '0') {
				mini.alert(data.message);
			} else {
				mini.alert("发生错误!");
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
			mini.unmask();
			mini.alert(jqXHR.responseText);
			CloseWindowLoseform();
		}
	});
}

////////////////////
//标准方法接口定义
function CheckEventLoseform(data) {
//跨页面传递的数据对象，克隆后才可以安全使用
	data = mini.clone(data);
	if (data.action == "edit") {
		SetDataLoseform(data);
	} else if (data.action == "new") {
	}else if(data.action =="look"){
		operType = data.action;
		SetDataLoseform(data);
		readOnlyForm();
	}
}

function GetDataLoseform(){
	var o = form.getData();
	var data = grid.getData();
	o.barcodeList = data;
	return o;
}
function SetDataLoseform(data){
		$.ajax({
			url : projectName+"/LoseformAdmin/queryByID?id="
					+ data.id,
			cache : false,
			success : function(text) {
				var o = mini.decode(text);
				form.setData(o);
				shopId.setText(o.shopName);
				storageId.setText(o.storageName);
				storagePhyId.setText(o.storagePhyName);
				inStorageId.setText(o.inStorageName);
				inStoragePhyId.setText(o.inStoragePhyName);
				grid.load({woNo:woNo.getValue()});
			}
			});
}
function CloseWindowLoseform(action) {
	if (action == "close" && form.isChanged()) {
		if (confirm("数据被修改了，是否先保存？")) {
			return false;
		}
	}
	if (window.CloseOwnerWindow)
		return window.CloseOwnerWindow(action);
	else
		window.close();
}
function onOkLoseform(e) {
	SaveDataLoseform();
}
function onCancelLoseform(e) {
	CloseWindowLoseform("cancel");
}

function labelModel() {
	var fields = form.getFields();
	for ( var i = 0, l = fields.length; i < l; i++) {
		var c = fields[i];
		if (c.setReadOnly)
			c.setReadOnly(true); //只读
		if (c.setIsValid)
			c.setIsValid(true); //去除错误提示
		if (c.addCls)
			c.addCls("asLabel"); //增加asLabel外观
	}
}
function inputModel() {
	var fields = form.getFields();
	for ( var i = 0, l = fields.length; i < l; i++) {
		var c = fields[i];
		if (c.setReadOnly)
			c.setReadOnly(false);
		if (c.removeCls)
			c.removeCls("asLabel");
	}
	mini.repaint(document.body);
}
function readOnlyForm(){

	var fields = form.getFields();
	for ( var i = 0, l = fields.length; i < l; i++) {
		var c = fields[i];
		if(c.setRequired){
			c.setRequired(false);
		}
		if(c.setReadOnly){
			c.setReadOnly(true);
		}
	}
	$("#addLoseform").hide();
	$("#removeLoseform").hide();
	$("#exportLoseform").hide();
	$("#submitId").hide();
	$("#button_okLoseform").hide();
	$("#button_onCancelLoseform").hide();
	$("#addloselist").hide();
	$("#deleteloselist").hide();
	
}

function add(){
	var orgId = mini.get("orgId").getValue();
	var woNo = mini.get("woNo").getValue();
	mini.open({
		url:projectName+"/LoseformAdmin/addLoseformEquipment",
		 title: "丢失设备清单", width: 700, height: 300,
		onload:function(){
			  var iframe = this.getIFrameEl();
			  var data = {'orgId':orgId,'woNo':woNo,eqNum:'1'};
			  iframe.contentWindow.SetData(data);
		},
		ondestroy: function (action) {
			if(action == "ok"){
				var iframe = this.getIFrameEl();
				var data = iframe.contentWindow.GetData();
				var griddata = grid.getData();
				if(griddata){
					for(var i=0; i<griddata.length; i++){
						if(data.eqType == griddata[i].eqType){
							mini.alert("不能添加重复类型");
							return ;
						}
					}
				}
				grid.addRow(data);
			}
        }
	});
}
function edit(){
	
}
function remove(){
	var rows = grid.getSelecteds();
	for(var i=0; i<rows.length;i++){
		 grid.removeRow(rows[i]);
	}
}


function onSelectStorage(e){
	if(grid.getData().length>0){
		mini.alert("请先删除子表数据再进行此操作");
		return;
	}
	var orgId = mini.get("orgId").getValue();
	var physicalStorageId = mini.get("storagePhyId").getValue();
	if(!physicalStorageId){
		mini.alert("请先选择仓库地点");
		return;
	}
    var btnEdit = this;
    mini.open({
        url: projectName+"/StStorageAdmin/stStorageChoose",
        title:"仓库选择", width: 780, height: 500,
        onload: function () {
            var iframe = this.getIFrameEl();
            var data = {physicalStorageId: physicalStorageId,orgId:orgId,chooseType:"true"};
            iframe.contentWindow.setData(data);
        },
        ondestroy: function (action) {
        	if (action == "ok") {
                var iframe = this.getIFrameEl();
                var data = iframe.contentWindow.GetData();
                data = mini.clone(data);
                var rows = data.rows;
                var ids = [], texts = [];
                for (var i = 0, l = rows.length; i < l; i++) {
                    var row = rows[i];
                    ids.push(row.storageId);
                    texts.push(row.storageName);
                }
                btnEdit.setValue(ids.join(","));
                btnEdit.setText(texts.join(","));
                storageName.setValue(texts.join(","));
                btnEdit.validate();
            }
        }
    });
}

function onSelectStoragePhy(e){
	if(grid.getData().length>0){
		mini.alert("请先删除子表数据再进行此操作");
		return;
	}
	var orgId = mini.get("orgId").getValue();
    var btnEdit = this;
    var v = btnEdit.getValue();
    mini.open({
        url: projectName+"/PhyStorageAdmin/phyStorageChoose",
        title:"仓库地点选择", width: 780, height: 500,
        onload: function () {
            var iframe = this.getIFrameEl();
            var data = {orgId: orgId,chooseType:"true",type:'0'};
            iframe.contentWindow.setData(data);
        },
        ondestroy: function (action) {
        	if (action == "ok") {
                var iframe = this.getIFrameEl();
                var data = iframe.contentWindow.GetData();
                data = mini.clone(data);
                var rows = data.rows;
                var ids = [], texts = [];
                for (var i = 0, l = rows.length; i < l; i++) {
                    var row = rows[i];
                    ids.push(row.physicalStorageId);
                    texts.push(row.physicalStorageName);
                }
                btnEdit.setValue(ids.join(","));
                btnEdit.setText(texts.join(","));
                storagePhyName.setValue(texts.join(","));
            	if(btnEdit.getValue()!=v){
					storageName.setValue("");
					storageId.setValue("");
					storageId.setText("");
				}
            	btnEdit.validate();
            }
        }
    });
}

function onSelectStorageIn(e){
	var orgId = mini.get("orgId").getValue();
	var physicalStorageId = mini.get("inStoragePhyId").getValue();
	if(!physicalStorageId){
		mini.alert("请先选择仓库地点");
		return;
	}
	var btnEdit = this;
	mini.open({
		url: projectName+"/StStorageAdmin/stStorageChoose",
		title:"仓库选择", width: 780, height: 500,
		onload: function () {
			var iframe = this.getIFrameEl();
			var data = {physicalStorageId: physicalStorageId,orgId:orgId,chooseType:"true",storageType:"4"};
			iframe.contentWindow.setData(data);
		},
		ondestroy: function (action) {
			if (action == "ok") {
				var iframe = this.getIFrameEl();
				var data = iframe.contentWindow.GetData();
				data = mini.clone(data);
				var rows = data.rows;
				var ids = [], texts = [];
				for (var i = 0, l = rows.length; i < l; i++) {
					var row = rows[i];
					ids.push(row.storageId);
					texts.push(row.storageName);
				}
				btnEdit.setValue(ids.join(","));
				btnEdit.setText(texts.join(","));
				inStorageName.setValue(texts.join(","));
				btnEdit.validate();
			}
		}
	});
}

function onSelectStoragePhyIn(e){
	
	var orgId = mini.get("orgId").getValue();
	var btnEdit = this;
	var v = btnEdit.getValue();
	mini.open({
		url: projectName+"/PhyStorageAdmin/phyStorageChoose",
		title:"仓库地点选择", width: 780, height: 500,
		onload: function () {
			var iframe = this.getIFrameEl();
			var data = {orgId: orgId,chooseType:"true",type:'0'};
			iframe.contentWindow.setData(data);
		},
		ondestroy: function (action) {
			if (action == "ok") {
				var iframe = this.getIFrameEl();
				var data = iframe.contentWindow.GetData();
				data = mini.clone(data);
				var rows = data.rows;
				var ids = [], texts = [];
				for (var i = 0, l = rows.length; i < l; i++) {
					var row = rows[i];
					ids.push(row.physicalStorageId);
					texts.push(row.physicalStorageName);
				}
				btnEdit.setValue(ids.join(","));
				btnEdit.setText(texts.join(","));
				inStoragePhyName.setValue(texts.join(","));
				if(btnEdit.getValue()!=v){
					inStorageName.setValue("");
					inStorageId.setValue("");
					inStorageId.setText("");
				}
				btnEdit.validate();
			}
		}
	});
}


function onButtoneShopEdit() {
	if(grid.getData().length>0){
		mini.alert("请先删除子表数据再进行此操作");
		return;
	}
	var orgId = mini.get("orgId").getValue();
    var btnEdit = this;
    mini.open({
        url: projectName+"/ShShopAdmin/shopChoose",
        title:"店铺选择", width: 780, height: 500,
        onload: function () {
            var iframe = this.getIFrameEl();
            var data = {orgId: orgId,chooseType:"true"};
            iframe.contentWindow.setData(data);
        },
        ondestroy: function (action) {
        	if (action == "ok") {
                var iframe = this.getIFrameEl();
                var data = iframe.contentWindow.GetData();
                data = mini.clone(data);
                var rows = data.rows;
                var row = rows[0];
                btnEdit.setValue(row.shopNumId);
                btnEdit.setText(row.shopName);
                shopName.setValue(row.shopName);
                btnEdit.validate();
            }
        }
    });
}

function onTypeChanged(e){
	if(grid.getData().length>0){
		grid.clearRows();
	}
	if(e.value == "1"){
		storageId.setValue("");
		storageName.setValue("");
		storageId.setText("");
		storageId.setEnabled(false);
		storageId.setRequired(false);
		storageId.validate();
		storagePhyId.setValue("");
		storagePhyName.setValue("");
		storagePhyId.setText("");
		storagePhyId.setEnabled(false);
		storagePhyId.setRequired(false);
		storagePhyId.validate();
		shopId.setEnabled(true);
		shopId.setRequired(true);
	}else if(e.value == "2"){
		// 店铺不可编辑
		shopId.setValue("");
		shopName.setValue("");
		shopId.setText("");
		shopId.setEnabled(false);
		shopId.setRequired(false);
		storageId.setEnabled(true);
		storageId.setRequired(true);
		storagePhyId.setEnabled(true);
		storagePhyId.setRequired(true);
		shopId.validate();
	}else if(e.value == "3"){
		storagePhyId.setEnabled(false);
		storagePhyId.setRequired(false);
		storagePhyId.setValue("");
		storagePhyId.setText("");
		storagePhyName.setValue("");
		
		storageId.setEnabled(false);
		storageId.setRequired(false);
		storageId.setValue("");
		storageId.setText("");
		storageId.setValue("");
		
		shopId.setEnabled(false);
		shopId.setRequired(false);
		shopId.setValue("");
		shopName.setValue("");
		shopId.validate();
		storagePhyId.validate();
		storageId.validate();
	}
}

function onHandle(e){
	var _uid = e.record._uid;
	var s = '<a style="color:blue;text-decoration:underline;"  onclick="viewBarcode(\''+_uid+'\')">查看条码</a> &nbsp;&nbsp;&nbsp;&nbsp;';
	if (operType == "look") {
		s+='<a style="color:blue;text-decoration:underline;display:none" onclick="editBarcode(\''+_uid+'\')">编辑条码</a>';
	}else{
		s+='<a style="color:blue;text-decoration:underline;" onclick="editBarcode(\''+_uid+'\')">编辑条码</a>';
	}	
	return s;
}

function viewBarcode(_uid){
	var row = grid.getRowByUID(_uid);
	var businessKey = mini.get("woNo").getValue();
	var storageNumId = inStorageId.getValue();
	mini.open({
		url:projectName+"/LoseformAdmin/viewBarcode",
		title:"查看条码", width: 780, height: 400,
		onload:function(){
			var iframe = this.getIFrameEl();
            var data = {eqType: row.eqType,eqTypeName:row.eqTypeName,businessKey:businessKey,storageNumId:storageNumId};
            iframe.contentWindow.setData(data);
		},destory:function(action){
			
		}
	});
}
function editBarcode(_uid){
	form.validate();
	if (form.isValid() == false)
		return;
	var row = grid.getRowByUID(_uid);
	var businessKey = mini.get("woNo").getValue();
	var storageNumId = inStorageId.getValue();
	var type = mini.get("type").getValue();
	if(!storageNumId){
		mini.alert("请选择接收仓库地点");
		return;
	}
	if(!type){
		mini.alert("请选择申请类型");
		return;
	}
	var srcStorageNumId;
	if(type==1){
		srcStorageNumId = shopId.getValue();
	}else if(type==2){
		srcStorageNumId = storageId.getValue();
	}else if(type==3){
		srcStorageNumId = "";
	}
	mini.open({
		url:projectName+"/LoseformAdmin/editBarcode",
		title:"查看条码", width: 780, height: 400,
		onload:function(){
			  var iframe = this.getIFrameEl();
	            var data = {eqType: row.eqType,eqTypeName:row.eqTypeName,businessKey:businessKey,storageNumId:storageNumId,checkBusKey:businessKey,type:type,srcStorageNumId:srcStorageNumId};
	            iframe.contentWindow.setData(data);
		},destory:function(action){
			
		}
	});
}
